<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/9/11
 * Time: 11:29
 */
class Custom_labelApp extends BackendApp{

    protected $_customLabel_mod;
    protected $_goods_mod;

    function __construct()
    {
        $this->Banner_settingApp();
    }

    function Banner_settingApp()
    {
        parent::BackendApp();
        $this->_customLabel_mod =& m('custom_label');
        $this->_goods_mod = & m("goods");
    }

    /***自定义标签列表*/
    function index(){
        $page = $this->_get_page();
        $res = $this->_customLabel_mod->find(array(
            'limit' => $page['limit'],
            'count' => true,
            'order' => "sort_order asc"
        )); //找出所有商城的合作伙伴
        $page['item_count'] = $this->_customLabel_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('res', $res);
        $this->assign('filtered', $filter? 1 : 0); //是否有查询条件
        $page['item_count'] = $this->_customLabel_mod->getCount();
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->display('custom_label.index.html');
    }
    /**添加自定义标签*/
    function add(){
        if(IS_POST){
            if($_POST['label_name'] == ''){
                $this->show_warning('标签名称必填！');
                return;
            }

            $data = array(
                'goods_id' => $_POST['goods_id'],
                'label_name' => $_POST['label_name'],
                'sort_order' => $_POST['sort_order'],
            );

            if (!$article_id = $this->_customLabel_mod->add($data)) {  //获取article_id
                $this->show_warning($this->_customLabel_mod->get_error());
                return;
            } else {
                $this->admin_log("新增自定义标签[" . $_POST['label_name'] . "]", 1);
            }
            $this->show_message('添加成功', 'back_list', 'index.php?app=custom_label', 'continue_add', 'index.php?app=custom_label&amp;act=add'
            );
        }else{
            $this->display('custom_label.form.html');
        }
    }

    /**编辑自定义标签*/
    function edit(){
        $ad_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$ad_id)
        {
            $this->show_warning('参数错误');
            return;
        }
        if(IS_POST){
            if($_POST['label_name'] == ''){
                $this->show_warning('标签名称必填！');
                return;
            }

            $goodsArr = explode(',',$_POST['goods_id']);
            $newGoodsArr = array_unique($goodsArr);
            $ids = '';
            foreach($newGoodsArr as $k=>$v){
                if($k == 0){
                    $ids = $v;
                }else{
                    $ids .= ','.$v;
                }
            }
            $data = array(
                'goods_id' => $ids,
                'label_name' => $_POST['label_name'],
                'sort_order' => $_POST['sort_order'],
            );
            $rows=$this->_customLabel_mod->edit($ad_id, $data);
            if ($this->_customLabel_mod->has_error())
            {
                $this->show_warning($this->_customLabel_mod->get_error());
                return;
            }else{
                $this->admin_log("修改自定义标签[".$_POST['label_name']."]",1);
            }

            $this->show_message('保存成功',
                'back_list',        'index.php?app=custom_label',
                '再次编辑',    'index.php?app=custom_label&amp;act=edit&amp;id=' . $ad_id);


        }else{
            $find_data     = $this->_customLabel_mod->find($ad_id);
            if (empty($find_data))
            {
                $this->show_warning('没有找到数据');
                return;
            }
            $ad = current($find_data);

            if($ad['goods_id']){
                $temp = explode(',',$ad['goods_id']);
                $temp_goods = array();
                foreach ($temp as $k => $v){
                    $temp_goods[$k]['goods_id'] = $v;
                    $c = $this->_goods_mod->get($v);
                    $temp_goods[$k]['goods_name'] = $c['goods_name'];
                    $temp_goods[$k]['default_image'] = dirname(site_url()) . "/" . $c['default_image'];
                }
                $ad['goods'] = $temp_goods;
            }
            $this->assign('res', $ad);
            $this->display('custom_label.form.html');
        }
    }

    /**删除标签**/
    function drop(){
        $article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$article_ids) {
            $this->show_warning('参数错误');

            return;
        }
        $article_ids = explode(',', $article_ids);
        $message = '删除成功';
        if (!$this->_customLabel_mod->drop($article_ids)) {    //删除
            $this->show_warning($this->_customLabel_mod->get_error());
            return;
        }else{
            $this->admin_log(Lang::get('del_banner'), 1);
        }
        $this->show_message($message);
    }


//异步修改数据
    function ajax_col()
    {
        $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data   = array();

        if (in_array($column ,array('sort_order')))
        {
            $data[$column] = $value;
            $this->_customLabel_mod->edit($id, $data);
            if(!$this->_customLabel_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return ;
        }
        return ;
    }






}